﻿/*
 * marqdown
 * --------
 * a C# Markdown processor
 * 
 * Copyright (c) 2010 Andrew Wall
 * http://code.google.com/p/marqdown/
 * 
 * 
 */


#region Copyright and license

/*

Copyright (c) 2010 Andrew Wall

 http://www.opensource.org/licenses/mit-license.php
  
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#endregion



namespace Marqdown
{
    namespace PostProcessing
    {
        public abstract class CommonState : State<PostProcessor>
        {
            public virtual bool newLine(PostProcessor info) { return true;  }
        }

        class PlainText : CommonState
        {
            private static CommonState escape = new Escape();
            private static CommonState italic = new Italic();
            private static CommonState backTick = new BackTick();
            private static CommonState link = new Link();
            public override void process(PostProcessor info, char c)
            {
                info.leadingChar = c;
                switch (c)
                {
                    case '\\':
                        info.ChangeState(escape);
                        break;
                    case '*':
                    case '_':
                        info.ChangeState(italic);
                        break;
                    case '`':
                        info.ChangeState(backTick);
                        break;
                    case '[':
                        info.ChangeState(link);
                        break;
                    default:
                        info.Append(c);
                        break;
                }
            }
        }

        internal class Escape : CommonState
        {
            public override void process(PostProcessor info, char c)
            {
                info.Append(c);
                info.ReturnToPlain();
            }
        }

        internal abstract class Emphasised : CommonState
        {
            protected void invertItalic(PostProcessor info)
            {
                info.italicOn = !info.italicOn;
            }
            protected void invertBold(PostProcessor info)
            {
                info.boldOn = !info.boldOn;
            }
            protected bool isItalic(PostProcessor info)
            {
                return info.italicOn;
            }
            protected bool isBold(PostProcessor info)
            {
                return info.boldOn;
            }
        }

        class Italic : Emphasised
        {
            private static CommonState bold = new Bold();
            internal static CommonState boldAndItalic = new BoldAndItalic();
            public override void process(PostProcessor info, char c)
            {
                if (c == info.leadingChar)
                {
                    info.ChangeState(bold);
                }
                else
                {
                    AppendMark(info);
                    info.Append(c);
                    info.ReturnToPlain();
                }
            }
            private void AppendMark(PostProcessor info)
            {
                if (isItalic(info))
                {
                    info.Append("</em>");
                }
                else
                {
                    info.Append("<em>");
                }
                invertItalic(info);
            }
        }

        class Bold : Emphasised
        {
            public override void process(PostProcessor info, char c)
            {
                if (c == info.leadingChar)
                {
                    info.ChangeState(Italic.boldAndItalic);
                }
                else
                {
                    AppendMark(info);
                    info.Append(c);
                    info.ReturnToPlain();
                }
            }
            private void AppendMark(PostProcessor info)
            {
                if (isBold(info))
                {
                    info.Append("</strong>");
                }
                else
                {
                    info.Append("<strong>");
                }
                invertBold(info);
            }
        }

        class BoldAndItalic : Emphasised
        {
            public override void process(PostProcessor info, char c)
            {
                AppendMark(info);
                info.Append(c);
                info.ReturnToPlain();
            }
            private void AppendMark(PostProcessor info)
            {
                if (isBold(info) && isItalic(info))
                {
                    info.Append("</em></strong>");
                }
                else
                {
                    info.Append("<strong><em>");
                }
                invertBold(info); invertItalic(info);
            }
        }

    }
}
